﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;

namespace CustomPixelShaderEffect.UlitFolder
{
    public static class Win2dUlit
    {
        public static void DrawGridGraphics(CanvasDrawingSession draw, int size = 100)
        {
            var target = new CanvasRenderTarget(draw, new Windows.Foundation.Size(size * 2, size * 2));
            using (var ds = target.CreateDrawingSession())
            {
                for (int x = 0; x < 2; x++)
                {
                    for (int y = 0; y < 2; y++)
                    {
                        Color color = (x + y) % 2 == 0 ? Colors.White : Color.FromArgb(255, 219, 219, 219);
                        ds.FillRectangle(new Rect(x * size, y * size, size, size), color);
                    }
                }
            }
            var gridTargetEffect = new BorderEffect()
            {
                Source = target,
                ExtendY = CanvasEdgeBehavior.Wrap,
                ExtendX = CanvasEdgeBehavior.Wrap,
            };
            draw.DrawImage(gridTargetEffect);
        }

        public static Transform2DEffect CalcutateImageCenteredTransform(double cWidth, double cHeight, double iWidth, double iHeight)
        {
            var mat = CalcutateImageCenteredMat(cWidth, cHeight, iWidth, iHeight);
            return new Transform2DEffect() { TransformMatrix = mat };
        }

        public static Transform2DEffect CalcutateImageCenteredTransform(Windows.Foundation.Size cSize, Windows.Foundation.Size iSize)
        {
            return CalcutateImageCenteredTransform(cSize.Width, cSize.Height, iSize.Width, iSize.Height);
        }

        public static Matrix3x2 CalcutateImageCenteredMat(Windows.Foundation.Size cSize, Windows.Foundation.Size iSize)
        {
            return CalcutateImageCenteredMat(cSize.Width, cSize.Height, iSize.Width, iSize.Height);
        }
        public static Matrix3x2 CalcutateImageCenteredMat(double cWidth, double cHeight, double iWidth, double iHeight)
        {
            float f = (float)Math.Min(cWidth / iWidth, cHeight / iHeight);
            float ox = (float)(cWidth - iWidth * f) / 2;
            float oy = (float)(cHeight - iHeight * f) / 2;
            Matrix3x2 matrix3X2 = Matrix3x2.CreateScale(f) * Matrix3x2.CreateTranslation(ox, oy);
            return matrix3X2;
        }
    }
}
